package com.mc2.docapp.disabilityassessmenttool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.mc2.docapp.disabilityassessmenttool.Misc.InputFilterMinMax;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.C_spin_affn_scoliosis_ext_points;

import java.util.HashSet;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;


public class ExtPointsScoliosis extends Activity {
    private static final int id = Activities.EXT_POINTS_SCOLIOSIS;
    EditText scoliosisExtPointsValue;
    private Realm realm;
    C_spin_affn_scoliosis_ext_points c_spin_affn_scoliosis_ext_points = new C_spin_affn_scoliosis_ext_points();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ext_points_scoliosis);

        scoliosisExtPointsValue = (EditText) findViewById(R.id.scoliosisExtPointsValue);
        initRealmDB();
        loadPrevData();
        sanitizeInputs();


        Button saveNnextButton = (Button) findViewById(R.id.saveAndNext);
        saveNnextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                c_spin_affn_scoliosis_ext_points.setExtra_points(Integer.parseInt(IfNull(scoliosisExtPointsValue.getText().toString(), "0")));

                realm.beginTransaction();
                realm.delete(C_spin_affn_scoliosis_ext_points.class);
                realm.insert(c_spin_affn_scoliosis_ext_points);
                realm.commitTransaction();

                NewPatient.finalDto.getClass_spinal_affection().getC_scoliosis_component().setSc_extra_points(c_spin_affn_scoliosis_ext_points);
                Intent intent = new Intent(ExtPointsScoliosis.this, router(ExtPointsScoliosis.class));
                startActivity(intent);
            }
        });

        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });

    }

    public String IfNull(String value, String defaultValue) {
        if(value == null || value.isEmpty()) {
            return defaultValue;
        } else {
            return  value;
        }
    }

    private void initRealmDB() {
        Realm.init(getApplicationContext());
        // realm = Realm.getDefaultInstance();
        RealmConfiguration config = new RealmConfiguration.Builder().name(id+".realm").build();
        realm = Realm.getInstance(config);
    }

    private void loadPrevData() {
        final RealmResults<C_spin_affn_scoliosis_ext_points> dtos = realm.where(C_spin_affn_scoliosis_ext_points.class).findAll();
//        .equalTo("id",id).findAll();
        System.out.println();
        try {
            //apply ui
            //
            //mapUI(new C_spin_affn_scoliosis_ext_points().strip(dtos.get(0)));
            //
            //realm.beginTransaction();
            //dtos.get(0).deleteFromRealm();
            //realm.commitTransaction();
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
            //no prev data found
        }
    }

    private void mapUI(C_spin_affn_scoliosis_ext_points oldData) {
        scoliosisExtPointsValue.setText(String.valueOf((long) (oldData.getExtra_points())));
    }


    public void sanitizeInputs() {
        scoliosisExtPointsValue.setFilters(new InputFilter[]{new InputFilterMinMax("0", "25")});
    }
}
